package com.dzxcqmxm.Dao.impl;

import com.dzxcqmxm.Dao.Dao;
import com.dzxcqmxm.Dao.UserDao;
import com.dzxcqmxm.JB.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 实现类
 */
public class UserDaoImpl implements UserDao {
    /**
     * 增加用户
     */
    @Override
    public void addUser(User user) throws Exception{
        //数据库插入数据
        String sql4 = "INSERT INTO `user` VALUES (?, ?);";
        int i = 0;
        try {
            //连接数据源
            Connection connection = getConnection();
            //得到语句对象
            PreparedStatement preparedStatement4 = connection.prepareStatement(sql4);
            //设置插入值
            preparedStatement4.setString(1,user.getUserName());
            preparedStatement4.setString(2,user.getUserPwd());

            //执行插入语句
            preparedStatement4.executeUpdate();

            //关闭连接，防止卡住！
            Dao.close(null,preparedStatement4,connection);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("插入数据失败！");
        }

    }

    /**
     * 删除用户
     */
    @Override
    public void delUser(String UserName) throws Exception {
        String sql = "DELETE FROM user WHERE UserName =?";

        try {
            Connection connection = getConnection();
            PreparedStatement pstm = connection.prepareStatement(sql);

            //设置要删除的语句，这个获取的id是就是上面那个where id啦
            pstm.setString(1,UserName);
            //执行了删除语句
            pstm.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败！！");

        }
    }


    /**
     *
     * 修改密码
     *
     */

    @Override
    public User updateUser(User user) throws Exception{
        String sql2 = "update user set UserPwd=? where UserName=?";
        Connection connection = getConnection();

        try {
            PreparedStatement preparedStatement4 = connection.prepareStatement(sql2);//得到语句对象
            preparedStatement4.setString(1,user.getUserPwd());
            preparedStatement4.setString(2,user.getUserName());

            preparedStatement4.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,preparedStatement4,connection);

        } catch (SQLException e) {
            System.out.println("更新数据失败!!!!");
            e.printStackTrace();
        }

        return user;
    }

    @Override
    public ArrayList<User> findAllUser() throws Exception {
        String sql4 = "select * from user";
        ArrayList<User> userArrayList = new ArrayList<User>();
        try {
            Connection connection = getConnection();
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql4);
            //执行查询语句（赋值给结果集）
            ResultSet rSt = preparedStatement2.executeQuery();
            while (rSt.next()){

                User user = new User();
                user.setUserName(rSt.getString("UserName"));
                user.setUserPwd(rSt.getString("UserPwd"));

                userArrayList.add(user);
            }
            //关闭连接，防止卡住！
            Dao.close(rSt,preparedStatement2,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询所有失败");
            return null;

        }
        return userArrayList;
    }

    /**
     * 通过用户名查找具体sql语句，返回User对象
     * @param UserName
     * @return
     * @throws Exception
     */
    @Override
    public User findUserByUserName(String UserName) throws Exception {

        String sql = "select * from user WHERE UserName=?";
        User user = new User();
        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,UserName);
        ResultSet rs = pstm.executeQuery();

        if(rs.next()){
            user.setUserName(rs.getString("UserName"));
            user.setUserPwd(rs.getString("UserPwd"));
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return user;
    }
}
